<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">{{ spu.spuName }}</el-form-item>
      <el-form-item label="sku名称">
        <el-input placeholder="sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="规格描述"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="(attr, index) in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="attrValue.valueName"
                :value="attr.id + ':' + attrValue.id"
                v-for="(attrValue, index) in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="(saleAttr, index) in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="saleAttr.id + ':' + saleAttrValue.id"
                v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
          >>
          <el-table-column type="selection" width="width"> </el-table-column>
          <el-table-column label="图片" width="width">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imageName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                v-if="!row.isDefault"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [], // spu图片
      spuSaleAttrList: [], // spu销售属性列表
      attrInfoList: [], // 属性信息列表
      // 收集sku数据
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [],
        //平台属性
        skuAttrValueList: [],
        //销售属性
        skuSaleAttrValueList: [],
      },
      spu: {},
      // 选中的图片
      imageList: [],
    };
  },
  methods: {
    // 获取sku数据
    async getData(category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spu = spu;

      // 发请求
      const resultList = await Promise.all([
        this.$API.spu.reqSpuImageList2(spu.id),
        this.$API.spu.reqSpuSaleAttrList(spu.id),
        this.$API.spu.reqAttrInfoList(
          category1Id,
          category2Id,
          spu.category3Id
        ),
      ]);

      if (resultList.every((result) => result.code === 200)) {
        resultList[0].data.forEach((v) => (v.isDefault = 0));
        [this.spuImageList, this.spuSaleAttrList, this.attrInfoList] =
          resultList.map((v) => v.data);
      }
    },
    // 点击取消
    cancel() {
      this.$emit("changeScene", { scene: 0, isAddSku: true });
      Object.assign(this._data, this.$options.data());
    },
    // 点击图片列表复选框
    handleSelectionChange(selected) {
      this.imageList = selected;
    },
    // 点击设置默认
    changeDefault(row) {
      this.spuImageList.forEach((v) => (v.isDefault = 0));
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    // 点击保存
    async save() {
      this.$emit("changeScene", { scene: 0, isAddSku: true });
      const { attrInfoList, spuSaleAttrList, imageList, skuInfo } = this;
      // 整理平台属性数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((pre, cur) => {
        if (cur.attrIdAndValueId) {
          const [attrId, valueId] = cur.attrIdAndValueId.split(":");
          pre.push({ attrId, valueId });
        }
        return pre;
      }, []);

      // 整理销售属性数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((pre, cur) => {
        if (cur.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = cur.attrIdAndValueId.split(":");
          pre.push({ saleAttrId, saleAttrValueId });
        }
        return pre;
      }, []);

      // 整理图片列表数据
      skuInfo.skuImageList = imageList.map((v) => ({
        imgName: v.imgName,
        imgUrl: v.imgUrl,
        isDefault: v.isDefault,
        spuImgId: v.id,
      }));

      let result = await this.$API.spu.reqAddSku(skuInfo);
      if (result.code === 200) {
        this.$message.success("添加sku成功");
        Object.assign(this._data, this.$options.data());
      }
    },
  },
};
</script>

<style></style>
